import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import UserLayout from '@/views/user/Layout.vue'
import AdminLayout from '@/views/admin/Layout.vue'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/course',
    component: () => import('../views/course/Layout.vue'),
    children: [
      {
        path: 'list',
        name: 'CourseList',
        component: () => import('../views/course/List.vue'),
        meta: { title: '课程列表' }
      },
      {
        path: ':id',
        name: 'CourseDetail',
        component: () => import('../views/course/Detail.vue'),
        meta: { title: '课程详情' }
      }
    ]
  },
  {
    path: '/category/:id',
    name: 'Category',
    component: () => import('../views/category/Index.vue'),
    meta: { title: '课程分类' }
  },
  {
    path: '/news',
    component: () => import('../views/news/Layout.vue'),
    children: [
      {
        path: '',
        name: 'NewsList',
        component: () => import('../views/news/List.vue'),
        meta: { title: '最新动态' }
      },
      // {
      //   path: ':id',
      //   name: 'NewsDetail',
      //   component: () => import('../views/news/Detail.vue'),
      //   meta: { title: '动态详情' }
      // }
    ]
  },
  {
    path: '/teacher',
    component: () => import('../views/teacher/Layout.vue'),
    children: [
      {
        path: '',
        name: 'TeacherList',
        component: () => import('../views/teacher/List.vue'),
        meta: { title: '讲师团队' }
      },
      {
        path: ':id',
        name: 'TeacherDetail',
        component: () => import('../views/teacher/Detail.vue'),
        meta: { title: '讲师详情' }
      }
    ]
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Search.vue'),
    meta: { title: '搜索结果' }
  },
  {
    path: '/user',
    component: UserLayout,
    children: [
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('../views/user/Profile.vue'),
        meta: { title: '个人资料', requiresAuth: true, requiresUser: true }
      },
      {
        path: 'settings',
        name: 'UserSettings',
        component: () => import('../views/user/Settings.vue'),
        meta: { title: '账号设置', requiresAuth: true, requiresUser: true }
      },
      {
        path: 'my-courses',
        name: 'my-courses',
        component: () => import('../views/user/MyCourses.vue'),
        meta: { requiresAuth: true, requiresUser: true }
      },
      {
        path: 'favorites',
        name: 'favorites',
        component: () => import('../views/user/Favorites.vue'),
        meta: { requiresAuth: true, requiresUser: true }
      },
      {
        path: 'learning-progress',
        name: 'learning-progress',
        component: () => import('../views/user/LearningProgress.vue'),
        meta: { requiresAuth: true, requiresUser: true }
      },
      {
        path: 'notes',
        name: 'notes',
        component: () => import('../views/user/Notes.vue'),
        meta: { requiresAuth: true, requiresUser: true }
      }
    ]
  },
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: 'profile',
        name: 'AdminProfile',
        component: () => import('../views/admin/Profile.vue'),
        meta: { title: '个人资料', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('../views/admin/Dashboard.vue'),
        meta: { title: '数据看板', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'settings',
        name: 'AdminSettings',
        component: () => import('../views/admin/Settings.vue'),
        meta: { title: '账号设置', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'user-manage',
        name: 'UserManage',
        component: () => import('../views/user/UserManage.vue'),
        meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'course-manage',
        name: 'CourseManage',
        component: () => import('../views/user/CourseManage.vue'),
        meta: { title: '课程管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'teacher-manage',
        name: 'TeacherManage',
        component: () => import('../views/user/TeacherManage.vue'),
        meta: { title: '讲师管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'system-settings',
        name: 'SystemSettings',
        component: () => import('../views/user/SystemSettings.vue'),
        meta: { title: '系统设置', requiresAuth: true, requiresAdmin: true }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    meta: { title: '404' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  console.log('Navigating to:', to.path, 'from:', from.path)
  console.log('Meta:', to.meta)
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 在线教育平台` : '在线教育平台'
  
  const isAuthenticated = localStorage.getItem('token')
  const userRole = localStorage.getItem('role')

  // 需要登录的页面
  if (to.meta.requiresAuth && !isAuthenticated) {
    ElMessage.warning('请先登录')
    next('/login')
    return
  }

  // 需要管理员权限的页面
  if (to.meta.requiresAdmin && userRole !== 'admin') {
    ElMessage.error('需要管理员权限')
    next('/home')
    return
  }

  // 需要普通用户权限的页面 (管理员不能访问)
  if (to.meta.requiresUser && userRole === 'admin') {
    ElMessage.error('管理员无权访问此页面')
    next('/admin/profile')
    return
  }

  // 已登录用户访问登录/注册页面时重定向
  if ((to.path === '/login' || to.path === '/register') && isAuthenticated) {
    if (userRole === 'admin') {
      next('/admin/profile')
    } else {
      next('/user/profile')
    }
    return
  }

  next()
})

export default router
